package br.unicamp.integralization.dto;

import java.util.ArrayList;
import java.util.List;

public class SubjectGroup implements Cloneable {

	private List<Subject> subjects;
	private int credits;

	public SubjectGroup() {
		subjects = new ArrayList<Subject>();
		credits = 0;
	}

	public void addHistorySubject(Subject subject) {
		subjects.add(subject);
		credits -= subject.getCredits();
	}

	public void addCatalogueSubject(Subject subject){
		subjects.add(subject);
		credits += subject.getCredits();
	}
	
	public SubjectGroup(List<Subject> subjects, int credits) {
		this.subjects = subjects;
		this.credits = credits;
	}

	public SubjectGroup(List<Subject> subjects2, int credits2, boolean deep) {
		this.credits = credits2;
		
		if(!deep){
			this.subjects = subjects2;
		}else{
			this.subjects = new ArrayList<Subject>(subjects2);
		}
	}

	public SubjectGroup clone() {
		return new SubjectGroup(subjects, credits,true);
	}
	
	public SubjectGroup deepClone(){
		return new SubjectGroup(subjects, credits,true);
	}
	

	public List<Subject> getSubjects() {
		return subjects;
	}

	public void setSubjects(List<Subject> subjects) {
		this.subjects = subjects;
	}

	public int getCredits() {
		return credits;
	}

	public void setCredits(int credits) {
		this.credits = credits;
	}

	public void removeSubject(Subject subject) {
		this.subjects.remove(subject);
		credits -= subject.getCredits();
	}
	
	@Override
	public String toString(){
		StringBuilder builder = new StringBuilder();
		builder.append("Total de créditos: " + getCredits()+ "\n");
		for (int i = 0; i < subjects.size(); i++) {
			builder.append(subjects.get(i).toString() + " ");
			if((i+1)%5==0){
				builder.append("\n");
			}
		}
		
		
		return builder.toString()+"\n";
	}

	public boolean matches(Subject subject) {
		for (Subject s : subjects) {
			if(s.matches(subject)){
				return true;
			}
		}
		return false;
	}

}
